﻿using System.Linq.Expressions;

namespace WinRTFramework.Common
{
	public static class PropertyNameResolver
	{
		public static string GetPropertyName( LambdaExpression propertyExpression )
		{
			MemberExpression memberExpr;
			switch ( propertyExpression.Body.NodeType )
			{
				case ExpressionType.Convert:
					memberExpr = ( (UnaryExpression)propertyExpression.Body ).Operand as MemberExpression;
					break;
				case ExpressionType.MemberAccess:
					memberExpr = propertyExpression.Body as MemberExpression;
					break;
				default:
					return null;
			}

			if ( memberExpr == null )
			{
				return null;
			}

			return memberExpr.Member.Name;
		}
	}
}